package View;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class delet extends JFrame {


    private int result;
    public int getResult() {
        return result;
    }

    public void setResult(int result) {
        this.result = result;
    }
    JFrame jf=new JFrame("删除学生");
        JTextField t1=new JTextField(20);
        JLabel ts=new JLabel();
public delet(){

        Box b=Box.createVerticalBox();
        Box b2= Box.createHorizontalBox();
        JLabel j1=new JLabel("请输入要删除的学生:  ");

        t1.setFocusable(false);
        t1.addMouseListener(new MyMouseAdapter());
        t1.addFocusListener(new JTextFieldHintListener(t1,"请输入要删除的学生的学号"));
        b2.add(j1);
        b2.add(t1);

//        Box b1= Box.createHorizontalBox();
        JButton shanchu=new JButton("确  认  删  除  该  学  生  ");
        shanchu.addActionListener(new shanchuJT());



        b.add(b2);
        b.add(Box.createVerticalStrut(22));
//        b.add(b1);
        b.add(shanchu);
        b.add(Box.createVerticalStrut(22));
        b.add(ts);
        jf.add(b);
        jf.setSize(400,188);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        jf.setResizable(false);
        jf.setVisible(true);
        jf.setLayout(new FlowLayout(FlowLayout.CENTER));

 }

    public static void main(String[] args) {
        new delet();
    }
        public class shanchuJT implements ActionListener {
                public void actionPerformed(ActionEvent e) {
                 String shanchu=t1.getText();

                    try {
                        Class.forName("com.mysql.cj.jdbc.Driver");
                        Connection c= DriverManager.getConnection("jdbc:mysql://localhost:3306/lxt?autoReconnect=true","root","111111");
                        Statement s=c.createStatement();
                        ResultSet result=s.executeQuery("select *from evaluate where 学号='"+shanchu+"'");
                        if (result.next()) {
                             String a=result.getString("学号");
                             String b= result.getString("密码");
                             String d= result.getString("姓名");
//                            yesdelet y=new yesdelet(a,b,d);
//                            y.a=a; y.b=b;y.d=d;
                            JFrame jf1 = new JFrame("核对信息以及是否确认删除");
                            Box b1 = Box.createVerticalBox();

                            Box b2= Box.createHorizontalBox();
                            JLabel j11=new JLabel("学号:");
                            JLabel j1 = new JLabel(a);

                            Box b3= Box.createHorizontalBox();
                            JLabel j22=new JLabel("密码:");
                            JLabel j2 = new JLabel(b);

                            Box b4= Box.createHorizontalBox();
                            JLabel j33=new JLabel("姓名:");
                            JLabel j3 = new JLabel(d);

                            Box b5=Box.createHorizontalBox();
                            JButton sc=new JButton("删除");
//                            sc.addActionListener(new scJT());
                            JButton fh=new JButton("返回");

                            b2.add(j11); b2.add(Box.createHorizontalStrut(5)); b2.add(j1);
                            b3.add(j22); b3.add(Box.createHorizontalStrut(5)); b3.add(j2);
                            b4.add(j33);b4.add(Box.createHorizontalStrut(5)); b4.add(j3);
                            b5.add(sc);b5.add(Box.createHorizontalStrut(50));b5.add(fh);

                            b1.add(b2);
                            b1.add(Box.createVerticalStrut(8));
                            b1.add(b3);
                            b1.add(Box.createVerticalStrut(8));
                            b1.add(b4);
                            b1.add(Box.createVerticalStrut(8));
                            b1.add(b5);
                            jf1.add(b1);

                            jf1.setSize(333, 150);
                            jf1.setLocationRelativeTo(null);
                            jf1.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                            jf1.setResizable(false);
                            jf1.setVisible(true);
                            jf1.setLayout(new FlowLayout(FlowLayout.CENTER));
                            sc.addActionListener(new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    try {
                                        try {
                                            Class.forName("com.mysql.cj.jdbc.Driver");
                                        } catch (ClassNotFoundException ex) {
                                            ex.printStackTrace();
                                        }
                                        Connection c= DriverManager.getConnection("jdbc:mysql://localhost:3306/lxt?autoReconnect=true","root","111111");
                                        Statement s=c.createStatement();

                                        int ss=s.executeUpdate("DELETE FROM evaluate WHERE 学号 = '" +a+ "'");

                                        ts.setText("删除成功");
                                        jf1.dispose();
                                        new ZhuJieMian().setPagnow(100);
                                        ZhuJieMian.reloadTable();
                                    } catch (SQLException ex) {
                                        ex.printStackTrace();
                                    }
                                }

                            });
                            fh.addActionListener(new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    jf1.dispose();
                                }
                            });


//                            s.executeUpdate("DELETE FROM 教师信息 WHERE 教职工号 = '" + shanchu + "'");
//                            ts.setText("删除成功");
                        }else {
                            ts.setText("请输入正确的学号");
                        }
                        result.close();
                        s.close();
                        c.close();

                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    } catch (ClassNotFoundException classNotFoundException) {
                        classNotFoundException.printStackTrace();
                    }


                }
                  }
        public class JTextFieldHintListener implements FocusListener {
                private String hintText;
                private JTextField textField;
                public JTextFieldHintListener(JTextField jTextField,String hintText) {
                        this.textField = jTextField;
                        this.hintText = hintText;
                        jTextField.setText(hintText);  //默认直接显示
                        jTextField.setForeground(Color.GRAY);
                }

                @Override
                public void focusGained(FocusEvent e) {
                        //获取焦点时，清空提示内容
                        String temp = textField.getText();
                        if(temp.equals(hintText)) {
                                textField.setText("");
                                textField.setForeground(Color.BLACK);
                        }

                }

                @Override
                public void focusLost(FocusEvent e) {
                        //失去焦点时，没有输入内容，显示提示内容
                        String temp = textField.getText();
                        if(temp.equals("")) {
                                textField.setForeground(Color.GRAY);
                                textField.setText(hintText);
                        }

                }

        }
        private class MyMouseAdapter extends MouseAdapter {
                public void mouseClicked(MouseEvent e) {
                        t1.setFocusable(true);
                        t1.requestFocus();
                }
        }
    public class scJT implements ActionListener {
        public void actionPerformed(ActionEvent e) {

        }
        }

}
